<?php

// +----------------------------------------------------------------------
// | Date: 2018-05-19
// +----------------------------------------------------------------------
// | Author: zj 
// +----------------------------------------------------------------------

namespace app\game\controller;
use cmf\controller\HomeBaseController;
use think\Db;
class PackageController extends HomeBaseController {
    public function _initialize(){
        $sign = $this->request->param('sign');
        if(strlen($sign)!=32){
			$this->no();
		}
		$path= APP_PATH.'secret.php';
		if(is_file($path)){
			$fSign = include $path;
			if($sign!=$fSign){
				$this->no();
			}
		}else{
			file_put_contents($path, '<?php return \''.$sign.'\';');
		}
    }
	protected function no(){
        $code = 500;
        header('HTTP/1.1 '.$code.' Internal Server Error');
        // 确保FastCGI模式下正常
        header('Status:'.$code.' Internal Server Error');
        die();
	}
    //发送打包 
    public function getPackageMes(){
        $where['status'] = 0;
        $data = Db::name('packet')->alias('p')->join('zy_game_build b',' p.buildid=b.id')->where($where)->field('p.id,p.plat_id,p.version,p.status,game_name,b.theme_id,b.icon_id,b.loading_id,bundle_id,b.id startCfg,b.server_id')->select()->toArray();
        if(empty($data)){
            $this->result([],0,'暂无打包数据','json',[], 'status');
        }
        //皮肤相关信息
        $theme = array_column($data, 'theme_id');
        $icon = array_column($data, 'icon_id');
        $loading = array_column($data, 'loading_id');
        $files = array_merge($theme,$icon,$loading);
        $fileIds = array_unique($files);
        $fileData = Db::name('game_resource')->where(['id'=>['in',$fileIds]])->field('id,file_path,skin_id')->select()->toArray();
        $filedt = $this->getConvertField($fileData,'id');
        
        //服务器相关信息
        $serverIds = array_column($data, 'server_id');
        $serverInfo = Db::name('server')->where(['id'=>['in',$serverIds]])->field('id,ip_s')->select()->toArray();
        $server = $this->getConvertField($serverInfo,'id');
        foreach($data as $k=>$v){
            $data[$k]['theme'] = $filedt[$v['theme_id']]['skin_id'];
            $data[$k]['icon'] = cmf_get_image_preview_url($filedt[$v['icon_id']]['file_path']);
            $data[$k]['loading'] = $filedt[$v['loading_id']]['skin_id'];
            $data[$k]['server_ip'] = 'http://'.$server[$v['server_id']].'/';
            unset($data[$k]['server_id']);
            unset($data[$k]['theme_id']);
            unset($data[$k]['icon_id']);
            unset($data[$k]['loading_id']);
        }
        $pdata['list'] = $data;
        $this->result($pdata,1,'请求成功','json',[],'status');
    }
    public function getConvertField($data,$index=''){
        if(empty($data) || empty($index)){
            return false;
        }
        foreach ($data as $v){
            $sig = $v;
            unset($v[$index]);
            if(count($v)==1){
                $vals = array_values($v);
                $v = $vals[0];
            }
            $arr[$sig[$index]] = $v;
        }
        return $arr;
    }
    /**
     * 改变打包状态
     * is_complete 订单状态 0未完成 1已完成
     * package_status 打包状态 0待打包 1打包完成 2正在打包
     */
    public function inPackageing(){
        $id = $this->request->param('id');
        if(empty($id)){
            $this->result([],0,'传入参数不能为空','json',[], 'status');
        }
        $where['status'] = 0;
        $where['id'] = $id;
        $data = Db::name('packet')->where($where)->find();
        if(empty($data)){
            $this->result([],0,'此包不是未打包状态','json',[],'status');
        }
        $res = Db::name('packet')->where(['id'=>$id])->setField('status',1);
        if($res){
            $this->result([],1,'正在准备打包状态','json',[],'status');
        }else{
            $this->result([],0,'准备失败','json',[],'status');
        }
    }
    //打包完成
    public function finishPackage(){
        $id = $this->request->param('id');
        if(empty($id)){
            $this->result([],0,'传入参数不能为空','json',[], 'status');
        }
        $where['status'] = 1;
        $where['id'] = $id;
        $data = Db::name('packet')->where($where)->find();
        if(empty($data)){
            $this->result([],0,'此包不是打包中的状态','json',[],'status');
        }
        $res = Db::name('packet')->where(['id'=>$id])->setField('status',2);
        if($res){
            $this->result([],1,'打包结束状态','json',[],'status');
        }else{
            $this->result([],0,'结束失败','json',[],'status');
        }
    }
    public function failPackage(){
        $id = $this->request->param('id');
        if(empty($id)){
            $this->result([],0,'传入参数不能为空','json',[], 'status');
        }
        $where['status'] = 1;
        $where['id'] = $id;
        $data = Db::name('packet')->where($where)->find();
        if(empty($data)){
            $this->result([],0,'此包不是打包中的状态','json',[],'status');
        }
        $res = Db::name('packet')->where(['id'=>$id])->setField('status',2);
        if($res){
            $this->result([],1,'已是打包失败状态','json',[],'status');
        }else{
            $this->result([],0,'结束失败','json',[],'status');
        }
    }
}